var fs = require("fs");
var express = require("express");
var server = express();
var expressStatic = require("express-static");
var mysql = require("mysql");
var url = require("url");
var sql = mysql.createConnection({
	host:"localhost",
	user:"root",
	password:"admin",
	database:"biyesheji"
})
var port = 81;
// 链接
sql.connect();
server.get("/register",(request,response)=>{
	var accountnumber1 = url.parse(request.url,true).query.accountnumber;
	var password1 = url.parse(request.url,true).query.password;
	var telephone1 = url.parse(request.url,true).query.telephone;
	var address1 = url.parse(request.url,true).query.address;
	var year1 = url.parse(request.url,true).query.year;
	if(accountnumber1  && accountnumber1 !== "0" && accountnumber1 !== "1" && accountnumber1 !== "2"){
		sql.query(`insert into biyeshejia (accountnumber,password,telephone,address,process,user,year) values("${accountnumber1}","${password1}","${telephone1}","${address1}","","用户","${year1}")`,function(error,data){
			if(error){
				console.log("注册失败")
				response.end("注册失败")
			} 
			else{
				console.log("注册成功")
				response.end("注册成功")
			}
		})
	}
	else if(accountnumber1 && password1 && accountnumber1 == "0" || accountnumber1 == "1" || accountnumber1 == "2"){
		console.log("此账号不能注册");
		response.end("注册失败")
	}
	else{
		console.log("信息不能为空（或账号已注册过）");
		response.end("注册失败")
	}
})
server.get("/modify",(request,response)=>{
	accountnumber1 = accountnumber2;
	var password1 = url.parse(request.url,true).query.password;
	var telephone1 = url.parse(request.url,true).query.telephone;
	var address1 = url.parse(request.url,true).query.address;
	if(accountnumber1){
		sql.query(`UPDATE biyeshejia SET password='${password1}',telephone='${telephone1}',address='${address1}' WHERE accountnumber="${accountnumber2}"`,function(error,data){
			if(error){
				console.log("修改失败")
				response.end("注册失败")
			} 
			else{
				console.log("修改成功")
				response.end("注册成功")
			}
		})
	}
	else{
		console.log("信息不能为空");
		response.end("修改失败")
	}
})
server.get("/sign-in",(request,response)=>{
	var accountnumber1 = url.parse(request.url,true).query.accountnumber;
	var password1 = url.parse(request.url,true).query.password;
	accountnumber2 = accountnumber1;
	if(accountnumber1 && password1 && accountnumber1 !== "0" && accountnumber1 !== "1" && accountnumber1 !== "2"){
		sql.query(`select accountnumber,password from biyeshejia where accountnumber="${accountnumber1}"`,function(error,data){
			if(data[0].password !== password1){
				console.log("登录失败")
				response.end("登录失败")
			} 
			else{
				console.log("登录成功")
				response.end("登录成功")
			}
		})
	}
	else if(accountnumber1 && password1 && accountnumber1 == "1" || accountnumber1 == "2"){
		sql.query(`select accountnumber,password from biyeshejia where accountnumber="${accountnumber1}"`,function(error,data){
			if(data[0].password !== password1){
				console.log("管理员登录失败")
				response.end("管理员登录失败")
			} 
			else{
				console.log("管理员登录成功")
				response.end("管理员登录成功")
			}
		})
	}
	else if(accountnumber1 && password1 && accountnumber1 == "0"){
		sql.query(`select accountnumber,password from biyeshejia where accountnumber="${accountnumber1}"`,function(error,data){
			if(data[0].password !== password1){
				console.log("超级管理员登录失败")
				response.end("超级管理员登录失败")
			} 
			else{
				console.log("超级管理员登录成功")
				response.end("超级管理员登录成功")
			}
		})
	}
	else{
		console.log("账号或密码不能为空");
		response.end("登录失败")
	}
})
server.get("/sign-success",(request,response)=>{
	sql.query(`select accountnumber from biyeshejia where accountnumber="${accountnumber2}"`,function(error,data){
		response.end(JSON.stringify(data[0].accountnumber))
	})
})
server.get("/order",(request,response)=>{
	remarks = url.parse(request.url,true).query.remarks;
	sql.query(`UPDATE biyeshejia SET remarks='${remarks}',process='未处理' WHERE accountnumber="${accountnumber2}"`,function(error,data){
		response.end("预约成功");
		console.log("预约成功");
	})
})
server.get("/ware-success",(request,response)=>{
	sql.query(`select accountnumber,remarks,process from biyeshejia where accountnumber="${accountnumber2}"`,function(error,data){
		response.end(JSON.stringify(data[0].accountnumber + "," + data[0].remarks + "," + data[0].process))
	})
})
server.get("/query",(request,response)=>{
	var process1 = url.parse(request.url,true).query.process;
	var user1 = url.parse(request.url,true).query.user;
	if(process1 && (process1 === "未处理" || process1 === "已处理" || process1 === "处理中")){
		console.log("管理员查询成功！")
		sql.query(`select accountnumber,telephone,remarks,address,process,user from biyeshejia where process="${process1}"`,function(error,data){
			response.end(JSON.stringify(data));
		})
	}
	else if(process1 && process1 === "用户"){
		console.log("管理员查询成功！!")
		sql.query(`select accountnumber,telephone,remarks,address,process,user from biyeshejia where user="${user1}"`,function(error,data){
			response.end(JSON.stringify(data));
		})
	}
	else{
		console.log("管理员查询失败！")
		response.end("error");
	}
})
server.get("/quer",(request,response)=>{
	var process1 = url.parse(request.url,true).query.process;
	var user1 = url.parse(request.url,true).query.user;
	if(process1 && process1 !== "用户"){
		console.log("超级管理员查询成功！")
		sql.query(`select accountnumber,telephone,remarks,address,process,user,year from biyeshejia where year="${process1}"`,function(error,data){
			response.end(JSON.stringify(data));
		})
	}
	else if(process1 && process1 === "用户"){
		console.log("超级管理员查询成功！!")
		sql.query(`select accountnumber,telephone,remarks,address,process,user,year from biyeshejia where user="${user1}"`,function(error,data){
			response.end(JSON.stringify(data));
		})
	}
	else{
		console.log("超级管理员查询失败！")
		response.end("error");
	}
})
server.get("/queries",(request,response)=>{
	var accountnumber1 = url.parse(request.url,true).query.accountnumber;
	var process1 = url.parse(request.url,true).query.process;
	sql.query(`UPDATE biyeshejia SET process='${process1}' WHERE accountnumber="${accountnumber1}"`,function(error,data){
		response.end("修改进度成功！");
		console.log("修改进度成功！");
	})
})
server.get("/removeUsers",(request,response)=>{
	var accountnumber1 = url.parse(request.url,true).query.accountnumber;
	accountnumber1 = accountnumber1.split(",");   // Split on commas 
	accountnumber1.forEach(function(item){
		sql.query(`delete from biyeshejia where accountnumber="${item}"`,function(error,data){
			response.end("超级管理员删除过期信息成功！");
			console.log("超级管理员删除过期信息成功！");
		})		
	})
})
server.use(expressStatic(`${__dirname}/static`));
//指明监听端口
server.listen(port)
console.log(`running of ${port}`)